package com.yj.springbootmybatis.service;

import com.yj.springbootmybatis.mapper.CategoryCustomMapper;
import com.yj.springbootmybatis.mapper.CategoryMapper;
import com.yj.springbootmybatis.pojo.Category;
import com.yj.springbootmybatis.pojo.vo.CategoryVo;
import com.yj.springbootmybatis.pojo.vo.NewItemsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private CategoryCustomMapper categoryCustomMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Category> queryOneLevel(Integer type) {

        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("type",type);

        List<Category> list = categoryMapper.selectByExample(example);

        return list;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<CategoryVo> getSubCatList(Integer fatherId) {

        List<CategoryVo> list = categoryCustomMapper.getSubCatList(fatherId);

        return list;
    }

    @Override
    public List<NewItemsVo> getSixNewItems(Integer rootCatId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("rootCatId",rootCatId);

        return categoryCustomMapper.getSixNewItems(map);
    }
}
